/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#pragma once



#include "hhdPlatform.h"
#include "hhdIMessageSender.h"
#include "hhdHashSet.hpp"

namespace hhd
{
    class IMessage;
    class IMessageReceiver;
    class IMessageSender;

    typedef void(*IMessageCallback)(IMessageReceiver*,IMessage*);

    class IMessageReceiver
    {
    protected:
        friend class Messenger;
        friend class IMessageSender;
        IMessageReceiver();
        virtual ~IMessageReceiver();

        static uint32_t _hash_pair(const Pair<int,IMessageSender*>& in_pair);
        static bool _equals_pair(const Pair<int,IMessageSender*>& left,
                                 const Pair<int,IMessageSender*>& right);

        template<typename T> void registerMessageCallback(IMessageCallback callback)
        {
            if(!_message_receiver_initialized)
            {
                _function_index = new HashTable<int, IMessageCallback>(2);
                _queued_messages_0 = new Vector<IMessage*>();
                _queued_messages_1 = new Vector<IMessage*>();
                _message_receiver_initialized = true;
                _senders = new HashSet< Pair< int, IMessageSender*> >(2, _hash_pair, _equals_pair);
            }

            int message_id = T::messageID();
            _function_index->add(message_id, callback);
        }
        virtual void handleMessages();
        void enqueueMessage(IMessage*);
        void swapQueues();

        // map message ids to message sender pointers
        HashSet< Pair< int, IMessageSender* > >* _senders;

        // map message ids to function pointers
        HashTable<int, IMessageCallback>* _function_index;

        // two queues of messages
        Vector<IMessage*>* _queued_messages_0;
        Vector<IMessage*>* _queued_messages_1;

        // lock for message buffer
        Mutex _queue_lock;

        bool _message_receiver_initialized;
    };
}
